package bar.dark.lee.cashdrawing;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import bar.dark.lee.BDConstents;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.CalendarPickerDialog;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.EditTextInputFilter;
import bar.dark.lee.common.RadioDialogActivity;
import bar.dark.lee.common.DBSQLite.ParseSelectData;
import bar.dark.lee.insert.BalanceQuery;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

public class CashPutActvitiy extends SuperActivity
{
	private static final int REQUEST_BANK = 0;
	private static final int REQUEST_CHECK = 1;
	private static final int REQUEST_CATEGORY = 4;
	
	private DBSQLite sql;
	
	/** 통장 라디오 버튼 */
	private RadioButton bankRadio;
	
	/** 체크카드 라디오 버튼 */
	private RadioButton checkRadio;
	
	/** 현금 잔액 */
	private TextView realMoney;
	
	/** 잔액 이름 텍스트 뷰 */
	private TextView orgMoneyText;	
	/** 잔액 텍스트 뷰 */
	private TextView orgMoney;
	
	/** 통장 뷰 */
	private View bankView;
	/** 통장 텍스트 뷰*/
	private TextView bankText;
	
	/** 체크카드 뷰 */
	private View checkView;
	/** 체크카드 텍스트 뷰*/
	private TextView checkText;
	
	/** 날짜 뷰 */
	private View dateView;
	/** 날짜 텍스트 뷰 */
	private TextView dateText;
	
	/** 카테고리 뷰 */
	private View categoryView;
	/** 카테고리 텍스트 뷰 */
	private TextView categoryTextView;
	
	/** 금액 */
	private EditText moneyEdit;
	
	/** 메모 뷰 */
	private View memoView;
	/** 메모 텍스트 뷰 */
	private EditText memoEdit;
	
	/** 인서트 버튼 */
	private Button insertButton;
	
	private ArrayList<String> bankList;
	private ArrayList<String> checkList;
	
	private ArrayList<String> categoryList;
	
	// 날짜
	private int toYear;
	private int toMonth;
	private int toDay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		sql = DBSQLite.getInstance(this);
		
		setContentView(R.layout.cashput_activity);
		
		setGoneTitle();
		
		setFindView();
		setOnClickListener();
		setData();
		
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
    	int flag = pref.getInt(BDConstents.OUTIN_MANAGER_SELECT_FLAG, 0);
    	
    	if (flag == 1)
    	{
    		setCheckRadioClick();
    	}
    	else
    	{
    		setBankRadioClick();
    	}
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		
		int flag = 0;
		
		if (checkRadio.isChecked())
		{
			flag = 1;
		}
		else
		{
			flag = 0;
		}
		
		editor.putInt(BDConstents.OUTIN_MANAGER_SELECT_FLAG, flag);
		
		editor.commit();
	}
	
	private void setFindView()
	{
		bankRadio = (RadioButton) findViewById(R.id.cashPutBankRadio);
		checkRadio = (RadioButton) findViewById(R.id.cashPutCheckRadio);
		realMoney = (TextView) findViewById(R.id.cashPutRealMoney);
		orgMoneyText = (TextView) findViewById(R.id.cashPutBalanceText);
		orgMoney = (TextView) findViewById(R.id.cashPutBalance);
		bankView = findViewById(R.id.cashPutBankView);
		bankText = (TextView) findViewById(R.id.cashPutBankKind);
		checkView = findViewById(R.id.cashPutCheckView);
		checkText = (TextView) findViewById(R.id.cashPutCheckKind);;
		dateView = findViewById(R.id.cashPutDate);
		dateText = (TextView) findViewById(R.id.cashPutDateTextView);
		categoryView = findViewById(R.id.cashPutCategory);
		categoryTextView = (TextView) findViewById(R.id.cashPutCategoryTextView);
		moneyEdit = (EditText) findViewById(R.id.cashPutMoney);
		EditTextInputFilter.filteringMoneyEditText(moneyEdit);
		memoView = findViewById(R.id.cashPutMemoView);
		memoEdit = (EditText) findViewById(R.id.cashPutMemo);
		insertButton = (Button) findViewById(R.id.cashPutInsertButton);
	}
	
	private void setBankRadioClick()
	{
		bankRadio.setChecked(true);
		checkRadio.setChecked(false);
		
		setBankView();
	}
	
	private void setCheckRadioClick()
	{
		checkRadio.setChecked(true);
		bankRadio.setChecked(false);
		
		setCheckView();
	}
	
	private void setOnClickListener()
	{
		bankRadio.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setBankRadioClick();
			}
		});
		
		checkRadio.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				setCheckRadioClick();
			}
		});
		
		bankView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(CashPutActvitiy.this, RadioDialogActivity.class);
				intent.putExtra(RadioDialogActivity.RADIODATA, bankList);
				intent.putExtra(RadioDialogActivity.RADIOSELECT, bankText.getText().toString());
				startActivityForResult(intent, REQUEST_BANK);
			}
		});
		
		checkView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(CashPutActvitiy.this, RadioDialogActivity.class);
				intent.putExtra(RadioDialogActivity.RADIODATA, checkList);
				intent.putExtra(RadioDialogActivity.RADIOSELECT, checkText.getText().toString());
				startActivityForResult(intent, REQUEST_CHECK);
			}
		});
		
		dateView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				CalendarPickerDialog cpd = new CalendarPickerDialog(superContext, new CalendarPickerDialog.ICalendarPickerDateSetListener() {
					@Override
					public void onDateSet(int year, int monthOfYear, int dayOfMonth) {
						
						dateText.setText(BDUtil.getDate(year, (monthOfYear+1), dayOfMonth));
						
						toYear = year;
						toMonth = monthOfYear;
						toDay = dayOfMonth;
					}
				}, toYear, toMonth, toDay);
				
				cpd.show();
			}
		});
		
		categoryView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(CashPutActvitiy.this, RadioDialogActivity.class);
				intent.putExtra(RadioDialogActivity.RADIODATA, categoryList);
				intent.putExtra(RadioDialogActivity.RADIOSELECT, categoryTextView.getText().toString());
				intent.putExtra(RadioDialogActivity.RADIOTYPE, RadioDialogActivity.CATEGORY_MOVE_TYPE);
				intent.putExtra(RadioDialogActivity.RADIOBUTTONSTRING, RadioDialogActivity.CATEGORY_MOVE_TYPE_STRING);
				startActivityForResult(intent, REQUEST_CATEGORY);
			}
		});
		
		insertButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (bankRadio.isChecked())
				{
					if ((Boolean) bankView.getTag() == false)
					{
						BDUtil.showDialogManager(superContext, "통장이 하나도 없습니다.");
						return;
					}
				}
				else
				{
					if ((Boolean) checkView.getTag() == false)
					{
						BDUtil.showDialogManager(superContext, "체크카드가 하나도 없습니다.");
						return;
					}
				}
				
				if (BDUtil.getCommaDel(moneyEdit.getText().toString()).equals(""))
				{
					BDUtil.showDialog(CashPutActvitiy.this, "알림", "금액을 입력해주세요.");
					return;
				}
				
				new ShowDialog().execute(BDUtil.asyncFlag);
			}
		});
	}
	
	private void setInsertData() throws Exception
	{
		HashMap<String, String> map = new HashMap<String, String>();
		map.put("inmoney", BDUtil.getCommaDel(moneyEdit.getText().toString()));
		map.put("outmoney", BDUtil.getCommaDel(moneyEdit.getText().toString()));
//		map.put("category", BDConstents.CATEGORY_MONEY_INOUT);
		map.put("category", categoryTextView.getText().toString());
		String date = dateText.getText().toString();
		date = date.replace(" ", "");
		date = date.replace("-", "");
		map.put("date", date);
		map.put("type", "6");
		
		String type = "";
		String moneyKind = "";
		
		if (bankRadio.isChecked())
		{
			map.put("moneykind", bankText.getText().toString());
			map.put("linktype", BDConstents.BANK_TYPE);
			
			type = BDConstents.BANK_TYPE;
			moneyKind = bankText.getText().toString();
		}
		else
		{
			map.put("moneykind", checkText.getText().toString());
			map.put("linktype", BDConstents.CHECK_TYPE);
			
			type = BDConstents.CHECK_TYPE;
			moneyKind = checkText.getText().toString();
		}
		
		map.put("linkid", -1+"");
		
		map.put("memo", memoEdit.getText().toString());
		
		sql.insert(this, map, "in_out");
		
		setRealMoney();
		
		setBankMoney();
		
		// 잔액을 입력 하기 전에 타입과 이름이 없으면 익셉션 발생
		if (type.equals("") || moneyKind.equals(""))
		{
			throw new Exception();
		}
		
		// 잔액을 입력한다.
		BalanceQuery balace = new BalanceQuery(superContext);
		balace.setBalance(type, moneyKind);
		
		// 카테고리 카운트 하나 증가
		sql.selectMapTile(this, "SELECT count FROM category_move_list WHERE kind = '" +
				BDUtil.getApostrophe(categoryTextView.getText().toString()) + "';", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int countIndex = c.getColumnIndex("count");
				int count = 0;
				
				c.moveToFirst();
				for (int i = 0; i < c.getCount(); i++)
				{
					count = c.getInt(countIndex);
					c.moveToNext();
				}
				
				count++;
				
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("count", "" + count);
				String [] whereArgs = new String[]{categoryTextView.getText().toString()};
				sql.update(superContext, "category_move_list", map, "kind = ?", whereArgs);
			}
		});
	}
	
	private void setBankMoney()
	{
		// 통장 금액 테이블 갱신
		sql.selectMapTile(this, "SELECT money FROM bank_money", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
//				int money = 0;
				BigDecimal money = BigDecimal.ZERO;
				int moneyIndex = c.getColumnIndex("money");
				
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
//					money += Integer.parseInt(c.getString(moneyIndex));
					money = money.add(new BigDecimal(c.getString(moneyIndex)));
					c.moveToNext();
				}
				
//				money = money + Integer.parseInt(moneyEdit.getText().toString());
				money = money.add(new BigDecimal(BDUtil.getCommaDel(moneyEdit.getText().toString())));
				
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("money", money.toString());
				sql.update(CashPutActvitiy.this, "bank_money", map, null, null);
			}
		});
		
		// 통장인지 체크카드인지 선택
		if (bankRadio.isChecked())
		{
			// 통장 테이블 갱신
			sql.selectMapTile(this, "SELECT id, money FROM bankbook WHERE name = '"
					+ BDUtil.getApostrophe(bankText.getText().toString()) + "';", new ParseSelectData()
			{
				@Override
				public void resultData(Cursor c)
				{
//					int money = 0;
					BigDecimal money = BigDecimal.ZERO;
					int id = -1;
					int idIdx = c.getColumnIndex("id");
					int moneyIndex = c.getColumnIndex("money");
					
					c.moveToFirst();
					
					for (int i = 0; i < c.getCount(); i++)
					{
						id = c.getInt(idIdx);
//						money += Integer.parseInt(c.getString(moneyIndex));
						money = money.add(new BigDecimal(c.getString(moneyIndex)));
						c.moveToNext();
					}
					
//					money = money + Integer.parseInt(moneyEdit.getText().toString());
					money = money.add(new BigDecimal(BDUtil.getCommaDel(moneyEdit.getText().toString())));
					
					HashMap<String, String> map = new HashMap<String, String>();
					map.put("money", money.toString());
					String [] whereArgs = new String[]{id + ""};
					sql.update(CashPutActvitiy.this, "bankbook", map, "id = ?", whereArgs);
				}
			});
		}
		else
		{
			// 통장 테이블 갱신
			sql.selectMapTile(this, "SELECT banklink FROM check_card WHERE name = '"
					+ BDUtil.getApostrophe(checkText.getText().toString()) + "';", new ParseSelectData()
			{
				@Override
				public void resultData(Cursor c)
				{
					int id = -1;
					int banklinkIdx = c.getColumnIndex("banklink");
					
					c.moveToFirst();
					
					for (int i = 0; i < c.getCount(); i++)
					{
						id = c.getInt(banklinkIdx);
						c.moveToNext();
					}
					
					if (id > -1)
					{
						setBankBookId(id);
					}
				}
			});
		}
	}
	
	private void setBankBookId(int linkId)
	{
		// 통장 테이블 갱신
		sql.selectMapTile(this, "SELECT id, money FROM bankbook WHERE id = "
				+ linkId + ";", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				BigDecimal money = BigDecimal.ZERO;
				int id = -1;
				int idIdx = c.getColumnIndex("id");
				int moneyIndex = c.getColumnIndex("money");
				
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					id = c.getInt(idIdx);
					money = money.add(new BigDecimal(c.getString(moneyIndex)));
					c.moveToNext();
				}
				
				money = money.add(new BigDecimal(BDUtil.getCommaDel(moneyEdit.getText().toString())));
				
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("money", money.toString());
				String [] whereArgs = new String[]{id + ""};
				sql.update(CashPutActvitiy.this, "bankbook", map, "id = ?", whereArgs);
			}
		});
	}

	private void setRealMoney()
	{
		sql.selectMapTile(this, "SELECT money FROM real_money;", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
//				int money = 0;
				BigDecimal money = BigDecimal.ZERO;
				int moneyIndex = c.getColumnIndex("money");
				
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
//					money += Integer.parseInt(c.getString(moneyIndex));
					money = money.add(new BigDecimal(c.getString(moneyIndex)));
					c.moveToNext();
				}
				
//				money = money - Integer.parseInt(moneyEdit.getText().toString());
				money = money.subtract(new BigDecimal(BDUtil.getCommaDel(moneyEdit.getText().toString())));
				
				HashMap<String, String> map = new HashMap<String, String>();
				map.put("money", money.toString());
				sql.update(CashPutActvitiy.this, "real_money", map, null, null);
			}
		});
	}

	private void setData()
	{
		bankRadio.setChecked(true);
		checkRadio.setChecked(false);
		
		setBankView();
		
		Calendar cal = Calendar.getInstance();
		toYear = cal.get(Calendar.YEAR);
		toMonth = cal.get(Calendar.MONTH);
		toDay = cal.get(Calendar.DAY_OF_MONTH);
		
		dateText.setText(BDUtil.getDate(toYear, toMonth+1, toDay));
		
		sql.selectMapTile(superContext, "SELECT * FROM real_money;", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int moneyIdx = c.getColumnIndex("money");
				
				c.moveToFirst();
				
				BigDecimal money = BigDecimal.ZERO;
				
				for (int i = 0; i < c.getCount(); i++)
				{
					money = money.add(new BigDecimal(c.getString(moneyIdx)));
					c.moveToNext();
				}
				
				realMoney.setText(BDUtil.getWon(money.toString()) + shape);
			}
		});
		
		sql.selectMapTile(this, "SELECT kind FROM category_move_list ORDER BY count DESC", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				categoryList = new ArrayList<String>();
				
				int nameIndex = c.getColumnIndex("kind");
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					String kind = c.getString(nameIndex);
					categoryList.add(kind);
					c.moveToNext();
				}
				
				if (categoryList.size() < 1)
				{
					String memo = "없음";
					categoryList.add(memo);
					categoryView.setClickable(false);
				}
				else
				{
					categoryView.setClickable(true);
				}
				
				categoryTextView.setText(categoryList.get(0).toString());
			}
		});
		
		sql.selectMapTile(this, "SELECT name FROM ( " +
								"SELECT b.name name " +
								"FROM check_card b, checkcard_detail d " +
								"WHERE b.id = d.linkid and d.count > 0 ORDER BY d.count) " +
								"UNION ALL " +
								"SELECT name FROM ( " +
								"SELECT b.name name " +
								"FROM check_card b, checkcard_detail d " +
								"WHERE b.id = d.linkid and d.count < 1 ORDER BY d.count);", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				checkList = new ArrayList<String>();
				
				int nameIndex = c.getColumnIndex("name");
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					String kind = c.getString(nameIndex);
					checkList.add(kind);
					c.moveToNext();
				}
				
				if (checkList.size() < 1)
				{
					String memo = "없음";
					checkList.add(memo);
					checkView.setClickable(false);
					checkView.setTag(false);
				}
				else
				{
					checkView.setClickable(true);
					checkView.setTag(true);
				}
				
				checkText.setText(checkList.get(0).toString());
			}
		});
		
		sql.selectMapTile(this, "SELECT name FROM ( " +
								"SELECT b.name name FROM bankbook b, bankbook_detail d " +
								"WHERE b.id = d.linkid and d.count > 0 ORDER BY d.count) " +
								"UNION ALL " +
								"SELECT name FROM ( " +
								"SELECT b.name name FROM bankbook b, bankbook_detail d " +
								"WHERE b.id = d.linkid and d.count < 1 ORDER BY d.count);", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				bankList = new ArrayList<String>();
				
				int nameIndex = c.getColumnIndex("name");
				c.moveToFirst();
				
				for (int i = 0; i < c.getCount(); i++)
				{
					String kind = c.getString(nameIndex);
					bankList.add(kind);
					c.moveToNext();
				}
				
				if (bankList.size() < 1)
				{
					String memo = "없음";
					bankList.add(memo);
					bankView.setClickable(false);
					bankView.setTag(false);
				}
				else
				{
					bankView.setClickable(true);
					bankView.setTag(true);
				}
				
				bankText.setText(bankList.get(0).toString());
			}
		});
		
		setBalanceBankMoney();
	}
	
	private void setBankView()
	{
		bankView.setVisibility(View.VISIBLE);
		checkView.setVisibility(View.GONE);
		
		setBalanceBankMoney();
	}
	
	private void setCheckView()
	{
		checkView.setVisibility(View.VISIBLE);
		bankView.setVisibility(View.GONE);
		
		setBalanceCheckMoney();
	}
	
	private void setBalanceBankMoney()
	{
		sql.selectMapTile(CashPutActvitiy.this, 
				"SELECT money FROM bankbook WHERE name = '" 
				+ BDUtil.getApostrophe(bankText.getText().toString()) + "';", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("money");
				c.moveToFirst();
//				int money = 0;
				BigDecimal money = BigDecimal.ZERO;
				for (int i = 0; i < count; i++)
				{
//					money = money + Integer.parseInt(c.getString(columIndex));
					money = money.add(new BigDecimal(c.getString(columIndex)));
					c.moveToNext();
				}
				
				orgMoneyText.setText("통장잔액");
				orgMoney.setText(BDUtil.getWon(money.toString()) + shape);
			}
		});
	}
	
	private void setBalanceCheckMoney()
	{
		sql.selectMapTile(this, "SELECT banklink FROM check_card WHERE name = '"
				+ BDUtil.getApostrophe(checkText.getText().toString()) + "';", new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				
				int columIndex = c.getColumnIndex("banklink");
				c.moveToFirst();
				int banklink = 0;
				for (int i = 0; i < count; i++)
				{
					banklink = c.getInt(columIndex);
					c.moveToNext();
				}
				
				sql.selectMapTile(CashPutActvitiy.this, "SELECT money FROM bankbook WHERE id = " + banklink, new ParseSelectData()
				{
					@Override
					public void resultData(Cursor c)
					{
						int count = c.getCount();
						
						int columIndex = c.getColumnIndex("money");
						c.moveToFirst();
//						int money = 0;
						BigDecimal money = BigDecimal.ZERO;
						for (int i = 0; i < count; i++)
						{
//							money = money + Integer.parseInt(c.getString(columIndex));
							money = money.add(new BigDecimal(c.getString(columIndex)));
							c.moveToNext();
						}
						
						orgMoneyText.setText("체크잔액");
						orgMoney.setText(BDUtil.getWon(money.toString()) + shape);
					}
				});
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == REQUEST_BANK)
		{
			if (resultCode == Activity.RESULT_OK)
			{
				String selectData = data.getStringExtra(RadioDialogActivity.RADIOSTRING);
				bankText.setText(selectData);
				
				setBalanceBankMoney();
			}
		}
		else if (requestCode == REQUEST_CHECK)
		{
			if (resultCode == Activity.RESULT_OK)
			{
				String selectData = data.getStringExtra(RadioDialogActivity.RADIOSTRING);
				checkText.setText(selectData);
				
				setBalanceCheckMoney();
			}
		}
		else if (requestCode == REQUEST_CATEGORY)
		{
			if (resultCode == Activity.RESULT_OK)
			{
				String selectData = data.getStringExtra(RadioDialogActivity.RADIOSTRING);
				categoryTextView.setText(selectData);
			}
			else if (resultCode == RadioDialogActivity.REFRESH_CATEGORY)
			{
				final String beforeCategory = categoryTextView.getText().toString();
				
				sql.selectMapTile(this, "SELECT kind FROM category_move_list ORDER BY count DESC", new ParseSelectData()
				{
					@Override
					public void resultData(Cursor c)
					{
						categoryList = new ArrayList<String>();
						
						int nameIndex = c.getColumnIndex("kind");
						c.moveToFirst();
						
						for (int i = 0; i < c.getCount(); i++)
						{
							String kind = c.getString(nameIndex);
							categoryList.add(kind);
							c.moveToNext();
						}
						
						if (categoryList.size() < 1)
						{
							String memo = "없음";
							categoryList.add(memo);
							categoryView.setClickable(false);
						}
						
						categoryTextView.setText(beforeCategory);
					}
				});
			}
		}
	}
	
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			if (BDUtil.btnEnable)
			{
				BDUtil.btnEnable = false;
				BDUtil.asyncFlag = -1;
				
				dialog = new ProgressDialog(CashPutActvitiy.this);
				dialog.setCancelable(false);
				dialog.setMessage("Loading...");
				dialog.show();
			}
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			if (arg0[0] == 0) 
			{
				try
				{
					sql.beginTansaction(superContext);
					
					setInsertData();
					
					sql.setTransactionSuccessful();
				} catch (Exception e)
				{
					return -1;
				} finally {
					try 
					{
						sql.endTransaction(superContext);
					} catch (Exception e) {
						return -1;
					}
				}
			}
			else
			{
				return arg0[0];
			}
			
			return arg0[0];
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			if (dialog != null) {
				dialog.dismiss();
			}
			
			if (result == -1)
			{
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
				
				BDUtil.showDialog(superContext, "오류", "데이터 저장에 실패했습니다.");
			}
			else if (result == 0) 
			{
				BDUtil.showDialogFinish(CashPutActvitiy.this, "알림", "현금입금이 처리 되었습니다.");
			}
		}
	}

	@Override
	protected void tipButton()
	{
	}
}
